package sis.ui;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-08-04 16:53
 * @notify
 * @version 1.0
 */

import sis.studentinfo.Course;
import sis.studentinfo.CourseDisplayAdapter;

import static java.awt.GridBagConstraints.*;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;

public class CoursesPanel extends JPanel {
    //标签内容
    public static final String LABEL_TEXT = "Courses";
    //该面板名称
    public static final String NAME = "coursesPanel";
    //标签名
    public static final String LABEL_NAME = "coursesLabel";
    //列表名
    public static final String COURSES_LIST_NAME = "coursesList";
    //按钮内容
    public static final String ADD_BUTTON_TEXT = "Add";
    //按钮名称
    public static final String ADD_BUTTON_NAME = "addButton";
    //课程名 输入框名
    public static final String DEPARTMENT_FIELD_NAME = "deptField";
    //课程编号 输入框名
    public static final String NUMBER_FIELD_NAME = "numberField";
    //课程名 标签名
    public static final String DEPARTMENT_LABEL_NAME = "deptLabel";
    //课程编号 标签名
    public static final String NUMBER_LABEL_NAME = "numberLabel";
    //课程名 标签内容
    public static final String DEPARTMENT_LABEL_TEXT = "Department";
    //课程编号 标签内容
    public static final String NUMBER_LABEL_TEXT = "Number";

    public static final String COURSES_LABEL_TEXT = "Courses";

    public static final String COURSES_LABEL_NAME = "coursesLabel";

    public static final char ADD_BUTTON_MNEMONIC = 'A';

    private JButton addButton;

    //列表项
    private DefaultListModel coursesModel = new DefaultListModel();


    public static void main(String[] args) {
        show(new CoursesPanel());
    }

    //获取列表值
    public Course getCourse(int index) {
        Course adapter = (CourseDisplayAdapter) coursesModel.getElementAt(index);
        return adapter;
    }

    //给文本框设置值
    public void setText(String textFieldName, String text) {
        getField(textFieldName).setText(text);
    }

    //显示窗体
    private static void show(JPanel panel) {
        //创建窗体
        JFrame frame = new JFrame();
        frame.setSize(300, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //获取窗体容器,并使用容器添加 面板
        frame.getContentPane().add(panel);
        frame.setVisible(true);
    }

    //构造子
    public CoursesPanel() {
        //设置面板名称
        setName(NAME);
        createLayout();
    }

    private void createLayout() {
        //列表
        JList coursesList = createList(COURSES_LIST_NAME, coursesModel);
        //创建一个滚动条,并且绑定一个 list显示框
        JScrollPane coursesScroll = new JScrollPane(coursesList);
        //设置滚动条样式
        coursesScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        //方位布局
        setLayout(new BorderLayout());

        //边框
        final int pad = 6;
        Border emptyBorder = BorderFactory.createEmptyBorder(pad, pad, pad, pad);
        Border bevelBorder = BorderFactory.createBevelBorder(BevelBorder.RAISED);
        Border titledBorder = BorderFactory.createTitledBorder(bevelBorder, COURSES_LABEL_TEXT);
        setBorder(BorderFactory.createCompoundBorder(emptyBorder, titledBorder));


        //中间 此处应该添加滚动条组件
        add(coursesScroll, BorderLayout.CENTER);
        //南边
        add(createBottomPanel(), BorderLayout.SOUTH);
    }

    JPanel createBottomPanel() {
        //按钮
        addButton = createButton(ADD_BUTTON_NAME, ADD_BUTTON_TEXT);
        //添加按钮助记码
        addButton.setMnemonic(ADD_BUTTON_MNEMONIC);
        //面板
        JPanel panel = new JPanel();
        //盒子布局
        panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));

        //空白行
        panel.add(Box.createRigidArea(new Dimension(0, 6)));
        //设置X对齐
        addButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        panel.add(addButton);

        //空白行
        panel.add(Box.createRigidArea(new Dimension(0, 6)));
        panel.add(createFieldsPanel());
        //设置间距
        panel.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));

        return panel;
    }

    //底部
    JPanel createFieldsPanel() {
        //灵活的网格布局
        GridBagLayout layout = new GridBagLayout();
        JPanel panel = new JPanel(layout);
        int columns = 20;

        addField(panel, layout, 0, DEPARTMENT_LABEL_NAME, DEPARTMENT_LABEL_TEXT,
                DEPARTMENT_FIELD_NAME, columns);
        addField(panel, layout, 1, NUMBER_LABEL_NAME, NUMBER_LABEL_TEXT,
                NUMBER_FIELD_NAME, columns);
        return panel;

    }

    //添加底部输入框
    private void addField(
            JPanel panel, GridBagLayout layout, int row,
            String labelName, String labelText,
            String fieldName, int fieldColumns) {

        JLabel label = createLabel(labelName, labelText);
        JTextField field = createField(fieldName, fieldColumns);

        Insets insets = new Insets(3, 3, 3, 3);

        layout.setConstraints(label,
                new GridBagConstraints(
                        0, row,
                        1, 1,
                        40, 1,
                        LINE_END,
                        NONE,
                        insets, 0, 0));

        layout.setConstraints(field,
                new GridBagConstraints(1, row,
                        2, 1, 60, 1, CENTER, HORIZONTAL,
                        insets, 0, 0));

        panel.add(label);
        panel.add(field);
    }

    //添加点击事件
    public void addCourseAddListener(ActionListener listener) {
        addButton.addActionListener(listener);
    }

    //添加课程
    public void addCourse(Course course) {
        coursesModel.addElement(new CourseDisplayAdapter(course));
    }

    //创建一个标签,并指定标签内容,名称
    private JLabel createLabel(String name, String text) {
        JLabel label = new JLabel(text);
        label.setName(name);
        return label;
    }

    //创建一个标签,并指定标签内容,名称
    private JList createList(String name, ListModel model) {
        JList list = new JList(model);
        list.setName(name);
        return list;
    }

    //创建一个按钮,并设置名称,内容
    private JButton createButton(String name, String text) {
        JButton label = new JButton(text);
        label.setName(name);
        return label;
    }

    //创建一个输入框,并设置名称,长度
    private JTextField createField(String name, int columns) {
        JTextField field = new JTextField(columns);
        field.setName(name);
        return field;
    }

    //根据标签名获取标签
    public JLabel getLabel(String name) {
        return (JLabel) Util.getComponent(this, name);
    }

    //根据列表名获取列表
    public JList getList(String name) {
        return (JList) Util.getComponent(this, name);
    }

    //根据按钮名获取按钮
    public JButton getButton(String name) {
        return (JButton) Util.getComponent(this, name);
    }

    //根据输入框名获取输入框
    public JTextField getField(String name) {
        return (JTextField) Util.getComponent(this, name);
    }

    //获取输入框的内容
    public String getText(String textFiledName) {
        return getField(textFiledName).getText();
    }

    //设置按钮是否可用
    public void setEnabled(String name, boolean state) {
        getButton(name).setEnabled(state);
    }

    //添加文本监听器
    public void addFieldListener(String name, KeyListener listener) {
        getField(name).addKeyListener(listener);
    }
}
